﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using wdss.WebServices;

namespace wdss.GUI
{
    public partial class ShareDocument : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1500;
            Response.CacheControl = "no-cache";
            username_lbl.Text = (string)Session["username"];
            username_lbl.Visible = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now);
            Response.Cache.SetNoServerCaching();
            Response.Cache.SetNoStore();
            if ((int)Session ["role"]==1)
            {
                Server.Transfer("~/GUI/Login.aspx");
            }
            if ((int)Session["role"] == 3)
            {
                Server.Transfer("~/GUI/SystemAdministrator.aspx");
            }
            if ((int)Session ["role"]==2)
            {
                Server.Transfer("~/GUI/DocumentList.aspx");
            }
        }
        protected void btn_Share_Click(object sender, EventArgs e)
        {
            Validate();
            lbl_Success.Visible = false;
            ShareDocumentService shareDocument = new ShareDocumentService();
            //no point if no access given
            if (cb_Read.Checked || cb_Update.Checked || cb_Check.Checked)
            {
                LogService logAction = new LogService();
                try
                {
                    string result = shareDocument.ShareDocument(txt_Username.Text.Trim(), (int)Session["userid"],
                        Int32.Parse(ddl_AuthoredDocuments.SelectedValue), cb_Read.Checked,
                        cb_Update.Checked, cb_Check.Checked);

                    if (result == "Success.")
                    {
                        logAction.LogAction("User " + (string)Session["username"] + " shared a document with " + txt_Username.Text.Trim() + ".");
                        lbl_Error.Visible = false;
                        lbl_Success.Visible = true;
                    }
                    else
                    {
                        logAction.LogAction("User " + (string)Session["username"] + " tried to share a document, but failed in the database for reason: " + result);
                        lbl_Error.Visible = true;
                        lbl_Error.Text = result;
                    }

                }
                catch (ArgumentNullException)
                {
                    lbl_Error.Visible = true;
                    lbl_Error.Text = "No document selected.";
                }
                catch (FormatException)
                {
                    lbl_Error.Visible = true;
                    lbl_Error.Text = "No document selected.";
                }
                catch (OverflowException)
                {
                    lbl_Error.Visible = true;
                    lbl_Error.Text = "No documet selected.";
                }
            }
        }
    }
}
